package com.epam.grsulab.webapi.web.pages.data;

import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.epam.grsulab.webapi.dao.base.Dao;
import com.epam.grsulab.webapi.entity.base.BaseEntity;
import com.epam.grsulab.webapi.tools.DataInitialiser;

/**
 * 
 * @author dmitry_zhivushko
 * @since 27.11.2013
 */
public class DataViewPage extends WebPage {

	@SpringBean
	private DataInitialiser dataInitialiser;

	public DataViewPage() {
		super();
		// get all DAOs defined in current project. It will be used to get
		// information about entities (it means that we can create tables and
		// columns on the page)
		final List<Dao<? extends BaseEntity>> allDAOs = dataInitialiser.getAllDAOs();

		// create custom wicket component (appropriate DIV exists in HTML. Try
		// to find it by "id")
		final WebMarkupContainer wmc = new WebMarkupContainer("listContainer");

		// create "list" component to populate data about each DB table (using data provided by DAO class)
		wmc.add(new ListView<Dao<? extends BaseEntity>>("list", allDAOs) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final ListItem<Dao<? extends BaseEntity>> item) {
				final Dao<? extends BaseEntity> dao = item.getModelObject();
				//check related HTML file and you will see all relations between components in JAVA and HTML
				item.add(new DBTableComponent("table", dao));
			}

		});
		wmc.setOutputMarkupId(true);
		add(wmc);

	}
}
